package cn.dansj.repository;

import cn.dansj.entity.Setting;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SettingRepository extends JpaRepository<Setting, String> {

    List<Setting> findAllByClassCodeOrderByRn(String classCode);

    @Query(value = "select t from Setting t order by t.classCode,t.rn")
    List<Setting> allSetting();

    Setting findBySequence(String sequence);

    @Query(value = "select t.classCode from Setting t group by t.classCode")
    List<String> getTitle();

    int countByClassCodeAndName(String classCode, String name);

    Setting findByClassCodeAndName(String classCode, String name);

    @Query(value = "update Setting t set t.rn = :rn where t.sequence = :sequence")
    @Modifying
    void updateSort(String sequence, String rn);

    @Query(value = "select max(t.rn) as rn from Setting t where t.classCode = :classCode")
    int getMaxRN(String classCode);
}
